// TypeScript file
class StarlingSwfUtils
{
    private static swfList: Array<starlingswf.Swf> = [];
    private static btnList: Array<any> = [];
    private static firstAddBtn: boolean = true;

    public static addSwf(swf: starlingswf.Swf): void
    {
        StarlingSwfUtils.swfList.push(swf);
    }

    public static removeSwf(swf: starlingswf.Swf): void
    {
        let idx: number = StarlingSwfUtils.swfList.indexOf(swf);
        if (-1 == idx)
        {
            return;
        }
        StarlingSwfUtils.swfList.splice(idx, 0);
    }

    public static createSprite(name: string, data: any[] = null, sprData: any[] = null): starlingswf.SwfSprite
    {
        let len: number = StarlingSwfUtils.swfList.length;
        for (let i: number = 0; i < 1; ++i)
        {
            let swf: starlingswf.Swf = StarlingSwfUtils.swfList[i];
            if (swf.hasSprite(name))
            {
                return swf.createSprite(name, data, sprData);
            }
        }
        return null;
    }
    
    public static createMovie(name: string, data: any[] = null, cls: any = null): starlingswf.SwfMovieClip
    {
        let len: number = StarlingSwfUtils.swfList.length;
        for (let i: number = 0; i < len; ++i)
        {
            let swf: starlingswf.Swf = StarlingSwfUtils.swfList[i];
            if (swf.hasMovieClip(name))
            {
                return swf.createMovie(name, data, cls);
            }
        }
        return null;
    }

    public static createImage(name: string, data: any[] = null): egret.Bitmap
    {
        let len: number = StarlingSwfUtils.swfList.length;
        for (let i: number = 0; i < len; ++i)
        {
            let swf: starlingswf.Swf = StarlingSwfUtils.swfList[i];
            if (swf.hasImage(name))
            {
                return swf.createImage(name, data);
            }
        }
        return null;
    }

    public static getTexture(name: string): egret.Texture
    {
        let len: number = StarlingSwfUtils.swfList.length;
        for (let i: number = 0; i < len; ++i)
        {
            let swf: starlingswf.Swf = StarlingSwfUtils.swfList[i];
            if (swf.hasImage(name))
            {
                return swf.getTexture(name);
            }
        }
        return null;
    }

    public static createS9Image(name: string, data: any[] = null): egret.Bitmap
    {
        let swfList: Array<starlingswf.Swf> = StarlingSwfUtils.swfList;
        let len: number = swfList.length;
        for (let i: number = 0; i < len; ++i)
        {
            let swf: starlingswf.Swf = swfList[i];
            if (swf.hasS9Image(name))
            {
                return swf.createS9Image(name, data);
            }
        }
        return null;
    }

    public static createShapeImage(name: string, data: any[] = null): egret.Bitmap
    {
        let swfList: Array<starlingswf.Swf> = StarlingSwfUtils.swfList;
        let len: number = swfList.length;
        for (let i: number = 0; i < len; ++i)
        {
            let swf:starlingswf.Swf = swfList[i];
            if (swf.hasShapeImage(name))
            {
                return swf.createShapeImage(name, data);
            }
        }
        return null;
    }

    public static getSwf(name: string): starlingswf.Swf
    {
        let swfList:Array<starlingswf.Swf> = StarlingSwfUtils.swfList;
        let len: number = swfList.length;
        for (let i: number = 0; i < len; ++i)
        {
            let swf: starlingswf.Swf = swfList[i];
            if (swf.name == name)
            {
                return swf;
            }
        }
        
        return null;
    }

    public static fixButton(btn: starlingswf.SwfMovieClip, onClick?: Function, thisObj?:any): void
    {
        if (StarlingSwfUtils.firstAddBtn)
        {
            StarlingSwfUtils.firstAddBtn = false;
            egret.MainContext.instance.stage.addEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this);
            egret.MainContext.instance.stage.addEventListener(egret.Event.LEAVE_STAGE, this.onTouchEnd, this);
        }  
        let data: StarlingSwfButtonData = new StarlingSwfButtonData();
        data.btn = btn;
        data.onClick = onClick;
        data.thisObj = thisObj;  
        StarlingSwfUtils.btnList.push(data);
        btn.touchEnabled = true;
        btn.gotoAndStop(0);
        btn.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onBtnTouchBegin, btn); 
    }

    private static onBtnTouchBegin(evt: egret.TouchEvent): void
    {
        let btn:starlingswf.SwfMovieClip = <starlingswf.SwfMovieClip>evt.currentTarget;
        let btnList: Array<any> = StarlingSwfUtils.btnList;
        let len: number = btnList.length;

        for (let i: number = 0; i < len; ++i)
        {
            let data: StarlingSwfButtonData = btnList[i];
            if (data.btn == btn)
            {
                data.touchStageX = evt.stageX;
                data.touchStageY = evt.stageY;
                btn.gotoAndStop(1);
                break;
            }
        }
    }

    private static onTouchEnd(evt: egret.TouchEvent): void
    {
        let btnList: Array<any> = StarlingSwfUtils.btnList;
        let len: number = btnList.length;
        for (let i: number = 0; i < len; ++i)
        {
            let data: StarlingSwfButtonData = btnList[i];
            if (-1 == data.touchStageX)
            {
                continue;
            }
            if (evt.stageX && Math.abs(data.touchStageX = evt.stageX) < 10 &&
            Math.abs(data.touchStageY - evt.stageY) < 10)
            {
                if (data.onClick)
                {
                    data.onClick.call(data.thisObj, evt);
                }

                data.touchStageX = -1;
                data.touchStageY = -1;
                data.btn.gotoAndStop(0);
            }
        }

    }
}

class StarlingSwfButtonData
{
    public btn: starlingswf.SwfMovieClip;
    public onClick: Function;
    public touchStageX: number = -1;
    public touchStageY: number = -1;
    public thisObj: any;
}